package models
import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
)


type Department struct {
	Id          	uint64		`orm:"unique;auto"`
	Name 			string
	Detail			string								//简介

}

func init() {
	// 需要在init中注册定义的model
	orm.RegisterModel(new(Department))
}


//通过ID获取部门数据
//@param 	jobID  string      用户的电话号码
//@return   user Admin      返回用户的数组
//@return   err	 error      返回的错误
func GetDepartmnetById(id uint64) (model Department,err error) {
	model = Department{Id: id}
	o := orm.NewOrm()
	err = o.Read(&model, "id")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
	}
	//fmt.Println(user.JobID,user.Passwd)
	beego.Info("user get department",model)
	return model,err
}

//获取所有的房间号
//@return   rooms []Room   返回admin的数组
//@return   count int64      数组的数量

func GetDepartmentList ()(departments []Department,count int64) {
	o := orm.NewOrm()
	department := new(Department)
	qs := o.QueryTable(department)
	qs.All(&departments)
	count, _ = qs.Count()
	return departments, count
}


//通过userID删除用户数据
//@param 	roomID   int64      用户的用户ID
//@return   	 	int64      返回删除数据的状态
//@return   err	 	error      返回的错误
func DelDepartmentByID(id uint64) (int64, error) {
	o := orm.NewOrm()
	model := Department{Id: id}

	status, err := o.Delete(&model)
	return status, err
}

//添加床位
//@param u  *Bed      要添加的用户数组结构体
//@return    int64      返回用户的id
//@return    error      返回的错误
func AddDepartment(u *Department) (int64, error) {
	o := orm.NewOrm()
	//	u.UserPw = Strtomd5(u.UserPw)
	BedID, err := o.Insert(u)
	return BedID, err
}




//更新床位
//@param 	u   Bed      更新用户的数组结构
//@return   	Bed      返回用户的数据
//@return   err	 error      返回的错误
func UpdateDepartment(u *Department) (Department, error) {
	department := Department{Id:u.Id}
	o := orm.NewOrm()
	err := o.Read(&department, "id")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
		return department,err
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
		return department,err
	}
	//	bed.RoomID = u.RoomID
	//	bed.Priority = u.Priority
	department.Detail = u.Detail
	department.Name = u.Name

	if num, err := o.Update(&department); err == nil {
		beego.Info(num)
	}else{
		beego.Info("Update",err)
	}
	beego.Info("Department",department)
	return department, err
}